package cn.joker.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.joker.auth.dto.LoginParamDTO;
import cn.joker.feign.RedisFeignClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
@Tag(name = "认证接口", description = "登录、登出、状态检查等")
public class AuthController {
    @Autowired
    private RedisFeignClient redisFeignClient;

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "通过用户名和密码进行登录，成功后返回 token 信息")
    public SaResult login(@RequestBody LoginParamDTO param) {
        // 自定义校验用户名密码（伪代码）
        if ("admin".equals(param.getUsername()) && "123456".equals(param.getPassword())) {
            StpUtil.login(param.getUsername());
            return SaResult.ok("登录成功").setData(StpUtil.getTokenInfo());
        }
        return SaResult.error("账号或密码错误");
    }

    @GetMapping("/isLogin")
    @Operation(summary = "检查是否登录", description = "判断当前是否处于登录状态")
    public SaResult isLogin() {
        return SaResult.ok().setData(StpUtil.isLogin());
    }


    @GetMapping("/redis-set")
    @Operation(summary = "redis测试", description = "通过feign调用redis服务")
    public SaResult redisSet() {
        String set = redisFeignClient.set();
        return SaResult.ok(set);
    }
}
